{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6d96e659-f743-40cb-bb49-d916a215aa4f",
   "metadata": {
    "tags": []
   },
   "source": [
    "# Conclusion: Best Practices and Future Trends in Options Trading"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0bfc0241-b312-45cd-b1b6-cfecab9ad85e",
   "metadata": {},
   "source": [
    "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d0d0add-ff03-4c37-af3c-3942c8adc582",
   "metadata": {
    "tags": []
   },
   "source": [
    "## Load the Libs we need"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4b0d19b3-7892-470c-8051-b3460605ed1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# import Lib\n",
    "import pandas as pd\n",
    "import datetime as dt\n",
    "import pytz\n",
    "import os\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "import yfinance as yf\n",
    "import scipy.stats as si\n",
    "import math\n",
    "import networkx as nx\n",
    "\n",
    "# import module\n",
    "from datetime import datetime, timezone\n",
    "from datetime import date, time\n",
    "from math import trunc\n",
    "from dateutil.parser import parse"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "36335255-3a12-4108-b282-d524a5e889cb",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "95f2519f-6f1b-4dd2-b10f-b1c834d88606",
   "metadata": {},
   "source": [
    "## Recap of Key Concepts and Best Practices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "850366eb-42e6-4fbc-9433-6c458d9d3788",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n",
      "                 Close     Hedge  Cumulative Hedge Returns\n",
      "Date                                                      \n",
      "2020-01-02   75.087502       NaN                  1.000000\n",
      "2020-01-03   74.357498  0.004861                  1.004861\n",
      "2020-01-06   74.949997 -0.003984                  1.000858\n",
      "2020-01-07   74.597504  0.002352                  1.003211\n",
      "2020-01-08   75.797501 -0.008043                  0.995142\n",
      "...                ...       ...                       ...\n",
      "2020-12-24  131.970001 -0.003856                  0.695213\n",
      "2020-12-28  136.690002 -0.017883                  0.682780\n",
      "2020-12-29  134.869995  0.006657                  0.687326\n",
      "2020-12-30  133.720001  0.004263                  0.690256\n",
      "2020-12-31  132.690002  0.003851                  0.692915\n",
      "\n",
      "[253 rows x 3 columns]\n"
     ]
    }
   ],
   "source": [
    "import yfinance as yf\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "# Fetching historical stock prices from Yahoo Finance\n",
    "ticker = \"AAPL\"  # Using Apple Inc. as an example. You can replace this with any valid ticker.\n",
    "data = yf.download(ticker, start=\"2020-01-01\", end=\"2021-01-01\")\n",
    "\n",
    "# Compute daily returns\n",
    "data['Returns'] = data['Close'].pct_change()\n",
    "\n",
    "# Assume an option's delta is 0.5\n",
    "delta = 0.5\n",
    "\n",
    "# Calculate required hedge for each day\n",
    "data['Hedge'] = -delta * data['Returns']\n",
    "\n",
    "# Compute cumulative returns of the hedge\n",
    "data['Cumulative Hedge Returns'] = np.cumprod(1 + data['Hedge'].fillna(0))\n",
    "\n",
    "print(data[['Close', 'Hedge', 'Cumulative Hedge Returns']])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a0211be9-0bb6-491b-90c3-17d487adfa73",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "53b003ca-2a23-466d-9b31-94b36656b0c5",
   "metadata": {},
   "source": [
    "## Machine Learning and AI in Options Valuation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "edafc802-cf5e-49d1-8c65-e182eb5dcdef",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[*********************100%***********************]  1 of 1 completed\n",
      "Mean Squared Error: 9.292765836247504\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import yfinance as yf\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_squared_error\n",
    "\n",
    "# Load data from Yahoo Finance for a specific stock (e.g., 'AAPL' for Apple Inc.)\n",
    "stock_symbol = 'AAPL'\n",
    "df = yf.download(stock_symbol, start=\"2020-01-01\", end=\"2023-01-01\")\n",
    "\n",
    "# Using past 5 days to predict the next day's closing price\n",
    "df['Target'] = df['Close'].shift(-1)\n",
    "df.dropna(inplace=True)\n",
    "\n",
    "# Feature columns: using Open, High, Low, Close as features\n",
    "features = df[['Open', 'High', 'Low', 'Close']]\n",
    "target = df['Target']\n",
    "\n",
    "X_train, X_test, y_train, y_test = train_test_split(features, target, test_size=0.2, random_state=42)\n",
    "\n",
    "model = LinearRegression()\n",
    "model.fit(X_train, y_train)\n",
    "\n",
    "predictions = model.predict(X_test)\n",
    "\n",
    "mse = mean_squared_error(y_test, predictions)\n",
    "print(f\"Mean Squared Error: {mse}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28218866-c929-4aff-816c-eb86526b7fcf",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
